package com.yoomiito.app.widget;

import android.content.Context;
import android.content.res.TypedArray;

import androidx.annotation.Nullable;
import androidx.appcompat.widget.AppCompatImageView;
import android.util.AttributeSet;
import android.view.View;

import com.yoomiito.app.R;
import com.yoomiito.app.listener.OnSelectedChangeListener;

/**
 * Created by Administrator on 2017/11/25 0025 13:36.
 */

public class SelectView extends AppCompatImageView implements View.OnClickListener {
    private Context context;
    private int selectedIconResId;
    private int unSelectedIconResId;
    private boolean isSelected = false;
    private boolean NoSelect = true;
    private OnSelectedChangeListener changeListener;
    private OnClickListener clickListener;

    public SelectView(Context context) {
        this(context, null);
    }

    public SelectView(Context context, @Nullable AttributeSet attrs) {
        this(context, attrs, 0);
    }

    public SelectView(Context context, @Nullable AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
        this.context = context;
        TypedArray ta = context.obtainStyledAttributes(attrs, R.styleable.SelectView);
        unSelectedIconResId = ta.getResourceId(R.styleable.SelectView_nSelectIcon, R.drawable.ic_collection);
        selectedIconResId = ta.getResourceId(R.styleable.SelectView_selectIcon, R.drawable.ic_collection_light);
        init(context);
    }

    private void init(Context context) {
        setOnClickListener(this);
        if (isSelected) {
            setImageResource(selectedIconResId);
        } else {
            setImageResource(unSelectedIconResId);
        }
        if (changeListener != null) {
            changeListener.change(this, isSelected);
        }
    }

    private void setResource(boolean flag) {
        if (flag) {
            setImageResource(selectedIconResId);
        } else {
            setImageResource(unSelectedIconResId);
        }
    }

    public SelectView setSelect(boolean selected) {
        isSelected = selected;
        setResource(selected);
//        if (changeListener!=null){
//            changeListener.change(this,selected);
//        }
        return this;
    }

    @Override
    public void onClick(View v) {
        if (NoSelect){
            if (isSelected) {
                isSelected = false;
            } else {
                isSelected = true;
            }
            setResource(isSelected);
        }
            if (changeListener != null) {
                changeListener.change(SelectView.this, isSelected);
            }


            if (clickListener != null) {
                clickListener.onClick(this);
            }

    }

    @Override
    public boolean isSelected() {
        return isSelected;
    }

    public SelectView setChangeListener(OnSelectedChangeListener changeListener) {
        this.changeListener = changeListener;
        return this;
    }

    public void setClickListener(OnClickListener clickListener) {
        this.clickListener = clickListener;
    }

    public void setUnClickSelect(int resId){
        setEnabled(false);
        setImageResource(resId);
    }

    public void cancelUnClickSelect(){
        setEnabled(true);
        setImageResource(unSelectedIconResId);
    }

    /**
     * 可点击 但不会更改选中状态
     * @param noSelect
     */
    public void setNoSelect(boolean noSelect) {
        NoSelect = noSelect;
    }
}
